package com.tinyengine.it.common.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class GenDir {

    // 路径常量
    public static final String optSourceTemplateDirLinux = FileUtil.FILE_SEPARATOR + "home" + FileUtil.FILE_SEPARATOR + "generator" + FileUtil.FILE_SEPARATOR + "templates";
    public static final String optSourceTemplateDirWindows = "D:" + FileUtil.FILE_SEPARATOR + "generator" + FileUtil.FILE_SEPARATOR + "templates";

    public static final String optTargetBladeXDirLinux = FileUtil.FILE_SEPARATOR + "home" + FileUtil.FILE_SEPARATOR + "generator" + FileUtil.FILE_SEPARATOR + "tmp" + FileUtil.FILE_SEPARATOR + "%s" + FileUtil.FILE_SEPARATOR + "BladeX";
    public static final String optTargetGitBladeXDirLinux = FileUtil.FILE_SEPARATOR + "home" + FileUtil.FILE_SEPARATOR + "generator" + FileUtil.FILE_SEPARATOR + "git" + FileUtil.FILE_SEPARATOR + "%s" + FileUtil.FILE_SEPARATOR + "BladeX";
    public static final String optTargetBladeXDirWindows = "D:" + FileUtil.FILE_SEPARATOR + "generator" + FileUtil.FILE_SEPARATOR + "tmp" + FileUtil.FILE_SEPARATOR + "%s" + FileUtil.FILE_SEPARATOR + "BladeX";

    public static final String templatesResourcesRootDir = getSpringOptSourcePathBasedOnOS() + FileUtil.FILE_SEPARATOR;

    // Vue 相关路径
    public static final String optTargetVueDirLinux = FileUtil.FILE_SEPARATOR + "home" + FileUtil.FILE_SEPARATOR + "generator" + FileUtil.FILE_SEPARATOR + "tmp" + FileUtil.FILE_SEPARATOR + "%s" + FileUtil.FILE_SEPARATOR + "saber3";
    public static final String optTargetGitVueDirLinux = FileUtil.FILE_SEPARATOR + "home" + FileUtil.FILE_SEPARATOR + "generator" + FileUtil.FILE_SEPARATOR + "git" + FileUtil.FILE_SEPARATOR + "%s" + FileUtil.FILE_SEPARATOR + "saber3";
    public static final String optTargetVueDirWindows = "D:" + FileUtil.FILE_SEPARATOR + "generator" + FileUtil.FILE_SEPARATOR + "tmp" + FileUtil.FILE_SEPARATOR + "%s" + FileUtil.FILE_SEPARATOR + "saber3";

    public static final String optTargetVueComponents = "src" + FileUtil.FILE_SEPARATOR + "components" + FileUtil.FILE_SEPARATOR;
    public static final String optTargetVueViews = "src" + FileUtil.FILE_SEPARATOR + "views" + FileUtil.FILE_SEPARATOR;
    public static final String optTargetVueRouter = "src" + FileUtil.FILE_SEPARATOR + "router" + FileUtil.FILE_SEPARATOR;

    public static final String srcMainDir = "src" + FileUtil.FILE_SEPARATOR + "main" + FileUtil.FILE_SEPARATOR + "java" + FileUtil.FILE_SEPARATOR;
    public static final String mainDir = "src" + FileUtil.FILE_SEPARATOR + "main" + FileUtil.FILE_SEPARATOR;
    public static final String packageDir = "org" + FileUtil.FILE_SEPARATOR + "springblade" + FileUtil.FILE_SEPARATOR + "%s" + FileUtil.FILE_SEPARATOR;

    public static final String resources = "resources";
    public static final String bladeService = "blade-service" + FileUtil.FILE_SEPARATOR;
    public static final String bladeServiceApi = "blade-service-api" + FileUtil.FILE_SEPARATOR;

    public static final String controllerDir = "controller";
    public static final String wrapperDir = "wrapper";
    public static final String mapperDir = "mapper";
    public static final String serviceDir = "service";
    public static final String entityDir = "entity";
    public static final String feignDir = "feign";
    public static final String voDir = "vo";
    public static final String apiTag = "-api";
    public static final String templatesDir = "generator" + FileUtil.FILE_SEPARATOR + "templates" + FileUtil.FILE_SEPARATOR + "templatesClass";

    public static String getSpringOptTargetPathBasedOnOS(Integer appId) {
        return getEnvSpringboot(optTargetBladeXDirWindows, optTargetBladeXDirLinux, appId);
    }

    public static String getSpringOptSourcePathBasedOnOS() {
        return getEnvSpringboot(optSourceTemplateDirWindows, optSourceTemplateDirLinux, null);
    }

    private static String getEnvSpringboot(String winPath, String linuxPath, Integer appId) {
        String osName = System.getProperty("os.name").toLowerCase();

        if (osName.contains("win")) {
            if (ObjUtil.isNotNull(appId)) {
                return String.format(winPath, appId);
            }
            return winPath;
        } else if (osName.contains("nix") || osName.contains("nux") || osName.contains("mac")) {
            if (ObjUtil.isNotNull(appId)) {
                return String.format(linuxPath, appId);
            }
            return linuxPath;
        } else {
            return "未知操作系统路径";
        }
    }

    public static String getVueOptTargetPathBasedOnOS(Integer appId) {
        return getEnvVue(optTargetVueDirWindows, optTargetVueDirLinux, appId);
    }

    private static String getEnvVue(String winPath, String linuxPath, Integer appId) {
        String osName = System.getProperty("os.name").toLowerCase();

        if (osName.contains("win")) {
            if (ObjUtil.isNotNull(appId)) {
                return String.format(winPath, appId);
            }
            return winPath;
        } else if (osName.contains("nix") || osName.contains("nux") || osName.contains("mac")) {
            if (ObjUtil.isNotNull(appId)) {
                return String.format(linuxPath, appId);
            }
            return linuxPath;
        } else {
            return "未知操作系统路径";
        }
    }
}
